﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using WS.EKA.Utility;

namespace WS.EKA.Web.UI
{
    public class PageBase : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated && !Request.Url.AbsolutePath.Contains("login.aspx")
                && !Request.Url.AbsolutePath.Contains("changepassword.aspx"))
            {
                Response.Redirect("/pages/login.aspx?returnUrl=" + Request.Url.AbsolutePath);
            }
        }

        #region alert frame
        protected void WebMessageBox(System.Web.UI.Page page, string msg, string callback = null)
        {
            if (string.IsNullOrEmpty(callback))
                PageRegisterStartupScript(page, string.Format("<script type=\"text/javascript\">ShowMessage('{0}')</script>", msg));
            else
                PageRegisterStartupScript(page, string.Format("<script type=\"text/javascript\">ShowMessage('{0}',{1})</script>", msg, callback));
        }

        protected void PageRegisterStartupScript(System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "", script);
        }
        #endregion

        #region login

        protected User GetLoginUser()
        {
            return XmlSerializerHelper.Deserialize<User>("User.xml");
        }

        [Serializable]
        public class User
        {
            [XmlElement]
            public string Name { get; set; }
            [XmlElement]
            public string Password { get; set; }
        }

        #endregion
    }
}